package com.H1023.controller;


import com.H1023.pojo.Lend;
import com.H1023.pojo.Reader;
import com.H1023.pojo.Remand;
import com.H1023.service.RemandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/Remand")
public class RemandController { //还书控制层
    @Autowired
    @Qualifier("remandServiceImpl")
    private RemandService remandService;


    @RequestMapping("/readerRemand")
    public String readerRemand(int bookid,int readerId){


            Lend lend=new Lend();
            lend.setReaderid(readerId);
            lend.setBookid(bookid);
            lend.setRemanddate(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
            System.out.println(lend);

            List<Lend> lendList = remandService.selectLenderById(lend);
            for (Lend lendObject : lendList) {
                if(lendObject.getState() == 1){ //如果在该书的借阅记录里面查询到了一条state=1的记录，说明这本书没还
                    remandService.updateRemandDate(lend);//插入
                    remandService.updateBookCount(bookid);//书籍数量加一
                    remandService.updateLendState(lendObject);//修改书籍状态位
                    return "redirect:/Lend/allBook";
                }
            }






        return "redirect:/Lend/allBook";
    }




}
